<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <div class="channel-wrap">
      <div class="title">
        <span>我的频道</span>
        <van-button
          size="small"
          plain
          type="danger"
          @click="isEdit=!isEdit"
        >{{isEdit?'取消':'编辑'}}</van-button>
      </div>

      <van-grid>
        <van-grid-item
          v-for="item,index in ownChannels"
          :key="item.id"
        >
          <span
            :class="{active:index==value}"
            class="size"
            @click="intoChannel(index)"
          >{{ item.name }}</span>
          <van-icon
            class="del-icon"
            name="cross"
            v-if="isEdit&&index"
            @click="Delete(item,index)"
          />
        </van-grid-item>
      </van-grid>
    </div>

    <!-- 可选频道 -->
    <div class="channel-wrap">
      <div class="title">
        <span>可选频道</span>
      </div>

      <van-grid>
        <van-grid-item
          v-for="item in optionalChannel"
          :key="item.id"
          :text="item.name"
          @click="addChannel(item)"
        />
      </van-grid>
    </div>
  </div>
</template>

<script>
import { addChannelAPI, DelChannelAPI } from '@/api'
import { setChannel } from '@/utils/storage'
export default {
  data() {
    return {
      isEdit: false, // XX关闭显示
      channels: [{
        id: '',
        seq: ''
      }]
    };
  },
  props: {
    ownChannels: { // 频道横列表
      type: Array,
      require: true
    },
    allChannels: { // 全部频道表格列表
      type: Array,
      require: true
    },
    value: { // 高亮的下标
      require: true,
      type: Number
    }
  },
  methods: {
    // 删除我的频道某一项
    async Delete(item, index) {
      this.ownChannels.splice(index, 1)
      if (this.value > index) {
        //   this.value不能--，因为产生单项数据流错误
        this.$emit('input', this.value - 1);
      }
      if (this.$store.state.tokenObj.token) { // 判断登录否
        const res = await DelChannelAPI(item.id)
      } else {
        // 没登录
        // 把剩余的我的频道数组保存到本地存储就行了
        setChannel(this.ownChannels)
      }
    },
    // 2. 点击可选添加频道
    async addChannel(item) {
      this.ownChannels.push(item)
      if (this.$store.state.tokenObj.token) { // 判断登录否
        await addChannelAPI({
          channels: [{
            id: item.id,
            seq: this.ownChannels.length + 1
          }]
        })
      } else {
        setChannel(this.ownChannels)
      }
    },
    // 传给父组件点击的下标及关闭
    intoChannel(index) {
      this.$emit('input', index)
      this.$emit('close')
    }
  },
  computed: {
    // 可选频道
    optionalChannel() {
      //   let res11 = this.allChannels.filter(item => {
      //     let value = this.ownChannels.find(val => {
      //       if (item.id === val.id) {
      //         return true
      //       }
      //     })
      //     // 如果下标为undefined代表不存在，那么就过滤出来
      //     if (value === undefined) {
      //       return true
      //     } else {
      //       return false
      //     }
      //   })
      return this.allChannels.filter(
        item => this.ownChannels.find(val => item.id === val.id) === undefined)
    }
  },
  created() {
  }
};
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 0 20px;
  .channel-wrap {
    .title {
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 0.32rem;
    }
    .size {
      font-size: 0.32rem;
    }
    .del-icon {
      position: absolute;
      top: 5px;
      right: 5px;
      font-size: 0.32rem;
    }
  }
  ::v-deep.active {
    color: red;
    font-size: 0.32rem;
  }
}
</style>